/*
 * tasklist.h
 *
 *  Created on: 2011-5-25
 *      Author: chenl
 */

#ifndef TASKLIST_H_
#define TASKLIST_H_

#include "common/common_def.h"
#include "global.h"

#include <ext/hash_map>

class CTask;

Interface ITaskList
{
	virtual ~ITaskList(){}
	virtual int32_t InsertTask(CTask&) = 0;
	virtual int32_t DeleteTask(const pid_t&) = 0;
	virtual CTask* GetTask(pid_t) = 0;
	virtual bool IsTaskOnList(pid_t) = 0;
	virtual bool IsEmpty() const = 0;
	virtual bool IsFull() const = 0;
};

class CTaskHashList : public ITaskList
{
protected:
	typedef __gnu_cxx ::hash_map<pid_t, CTask*, __gnu_cxx::hash<pid_t>,
	                        __gnu_cxx::equal_to<pid_t>, DEFAULT_ALLOC<CTask*> > TaskHashList;
	typedef TaskHashList::iterator TaskHashListItr;
	typedef TaskHashList::const_iterator ConstTaskHashLisItr;

public:
	CTaskHashList(){}
	virtual ~CTaskHashList(){}

public:
	virtual int32_t InsertTask(CTask& task);

	virtual int32_t DeleteTask(const pid_t& pid)
	{
		taskHashList_.erase(pid);
		return S_OK;
	}

	virtual CTask* GetTask(pid_t pid)
	{
		TaskHashListItr itr = FindTask(pid);
		if (taskHashList_.end() == itr)
		{
			return NULL;
		}
		return itr->second;
	}
	virtual bool IsTaskOnList(pid_t pid)
	{
		return taskHashList_.end() != FindTask(pid);
	}
	virtual bool IsEmpty() const
	{
		return taskHashList_.empty();
	}
	virtual bool IsFull() const
	{
		return false;
	}

protected:
    TaskHashListItr FindTask(pid_t pid)
    {
    	TaskHashListItr itr = taskHashList_.find(pid);
    	return itr;
    }

protected:
	TaskHashList taskHashList_;
};

#endif /* TASKLIST_H_ */
